#encoding=utf-8
from __future__ import unicode_literals
from django.contrib.auth.models import AbstractUser
from django.conf import settings
from django.db import models
import datetime
from django.utils import timezone
# Create your models here.
class Protocol(models.Model):
    """通讯协议"""
    protocol_id      = models.IntegerField(primary_key = True)
    protocol_model   = models.CharField(max_length = 20)
    protocol_version = models.CharField(max_length = 20)
    protocol_content = models.CharField(max_length = 1023)

    def __unicode__(self):
        return self.protocol_content

class Management(models.Model):
    """经营管理人"""
    company  = models.CharField(max_length = 20, null = True, blank = True)
    phone    = models.CharField(max_length = 20, null = True, blank = True)
    email    = models.EmailField(max_length = 50, null = True, blank = True)
    fax      = models.CharField(max_length = 20, null = True, blank = True)
    contacts = models.CharField(max_length = 20, null = True, blank = True)

    def __unicode__(self):
        return self.company

class Ship_owner(models.Model):
    """船东"""
    ship_owner_id        = models.IntegerField(primary_key = True)
    ship_owner_name      = models.CharField(max_length = 255, null = True, blank = True)
    ship_owner_contacter = models.CharField(max_length =255, null = True, blank = True)
    phone                = models.CharField(max_length = 255, null = True, blank = True)
    address              = models.CharField(max_length = 255, null = True, blank = True)
    email                = models.EmailField(max_length = 50, null = True, blank = True)
    fax                  = models.CharField(max_length = 20, null = True, blank = True)

    def __unicode__(self):
        return self.ship_owner_name

class Ship_type(models.Model):
    """船舶类型"""
    ship_type_name_chinese  = models.CharField(max_length = 10, null = True, blank = True)
    ship_type_name_english  = models.CharField(max_length = 20, null = True, blank = True)
    notes                   = models.CharField(max_length = 255, null = True, blank = True)
    level                   = models.IntegerField(null = True, blank = True)

    def __unicode__(self):
        return self.ship_type_name_english

class Ship(models.Model):
    """船"""
    ship_id                 = models.IntegerField(primary_key  = True)
    user_id                 = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='user_about_ship')
    protocol_id             = models.ForeignKey(Protocol, related_name='protocol_about_ship')
    name                    = models.CharField(max_length = 100, null = True, blank = True)
    name_chinese            = models.CharField(max_length = 50, null = True, blank = True)
    imo                     = models.CharField(max_length = 50, null = True, blank = True)#imo 是什么
    mmsi                    = models.CharField(max_length = 50, null = True, blank = True)
    call_sign               = models.CharField(max_length = 50, null = True, blank = True)
    ship_owner_id           = models.ForeignKey(Ship_owner, related_name='ship_owner_about_ship')
    flag                    = models.CharField(max_length = 50, null = True, blank = True)
    ship_type_id            = models.ForeignKey(Ship_type, related_name='ship_type_about_ship')
    ship_length             = models.FloatField(null = True, blank = True)
    ship_width              = models.FloatField(null = True, blank = True)
    draft                   = models.FloatField(null = True, blank = True)
    total_weight            = models.FloatField(null = True, blank = True)
    load                    = models.FloatField(null = True, blank = True)
    net_weight              = models.FloatField(null = True, blank = True)
    build_time              = models.DateField(auto_now = False, null = True, blank = True)
    photo                   = models.CharField(max_length = 50, null = True, blank = True)
    ship_society            = models.CharField(max_length = 50, null = True, blank = True)
    ship_build_planet       = models.CharField(max_length = 50, null = True, blank = True)
    ship_build_location     = models.CharField(max_length = 100, null = True, blank = True)
    ship_build_time         = models.DateField(auto_now = False, null = True, blank = True)
    launching_time          = models.DateField(auto_now = False, null = True, blank = True)
    manager_id              = models.ForeignKey(Management, related_name='manager_about_ship')
    cumulative_sailing_time = models.IntegerField(null = True, blank = True)

    def __unicode__ (self):
        return self.name

class Ship_data(models.Model):
    """船舶数据"""
    receive_data_id        = models.IntegerField(primary_key = True)
    ship_id                = models.ForeignKey(Ship, related_name='ship_about_ship_data')
    generation_data_time   = models.DateTimeField(auto_now = False)
    receive_data_time      = models.DateTimeField(auto_now = False)
    gps_status             = models.IntegerField()
    used_satellite_number  = models.IntegerField()
    longitude              = models.DecimalField(max_digits=9, decimal_places=2)
    latitude               = models.DecimalField(max_digits=9, decimal_places=2)
    altitude               = models.DecimalField(max_digits=5, decimal_places=2)
    speed                  = models.FloatField()
    angle                  = models.SmallIntegerField()
    accumulated_mileage    = models.IntegerField(null = True, blank = True)
    accumulated_consum_oil = models.IntegerField(null = True, blank = True)
    otherdate              = models.CharField(max_length = 255)

class Ship_group(models.Model):
    """船舶分组"""
    ship_group_id = models.IntegerField(primary_key = True)
    ship_group    = models.CharField(max_length = 255)

    def __unicode__(self):
        return self.ship_group

class User_type(models.Model):
    """用户类型"""
    user_type_id = models.IntegerField(primary_key = True)
    type_name    = models.CharField(max_length = 10)

    def __unicode__(self):
        return self.type_name

class NewUser(AbstractUser):
    user_alias                = models.CharField(max_length = 20)

class User(models.Model):
    user_alias                = models.CharField(max_length = 20)
    email                     = models.EmailField(max_length = 20)
    password                  = models.CharField(max_length = 20)
    user_type                 = models.ForeignKey(User_type, related_name='user_type_about_user')
    real_name                 = models.CharField(max_length = 20, null = True, blank = True)
    fixed_telephone           = models.CharField(max_length = 20, null = True, blank = True)
    mobile_phone              = models.CharField(max_length = 20)
    registration_time         = models.DateTimeField(auto_now = False)
    preferred_language        = models.CharField(max_length = 10)
    preferred_unit            = models.IntegerField(null = True, blank = True)
    preferred_map             = models.IntegerField(null = True, blank = True)
    last_login_time           = models.DateTimeField(auto_now = True)
    last_login_ip             = models.CharField(max_length = 40)
    password_recovery_problem = models.CharField(max_length = 255, null = True, blank = True)
    password_recovery_answer  = models.CharField(max_length = 255, null = True, blank = True)

    def __unicode__(self):
        return self.user_alias

class Relation_ship(models.Model):
    """用户船舶关系"""
    fleet_name_id = models.ForeignKey(Ship_group, related_name='fleet_name_about_relation_ship')
    ship_id       = models.ForeignKey(Ship, related_name='ship_about_relation_ship')
    user_id       = models.ForeignKey(User, related_name='user_about_relation_ship')

class Phone_card(models.Model):
    """电话卡"""
    telephone_number = models.CharField(max_length =18)
    query_password   = models.CharField(max_length = 12, null = True, blank = True)
    flow             = models.IntegerField(null = True, blank = True)
    opening_date     = models.DateTimeField(auto_now = False, null = True, blank = True)
    maturity_date    = models.DateTimeField(auto_now = False, null = True, blank = True)
    balance          = models.DecimalField(max_digits=6, decimal_places=2, null = True, blank = True)
    used_flow        = models.IntegerField(null = True, blank = True)

    def __unicode__(self):
        return self.telephone_number

class Terminal(models.Model):
    """船舶终端"""
    terminal_id             = models.IntegerField(primary_key = True)
    ship_id                 = models.ForeignKey(Ship, related_name='ship_about_terminal')
    phone_card_id           = models.ForeignKey(Phone_card, related_name='phone_card_about_terminal')
    terminal_model          = models.CharField(max_length = 20, null = True, blank = True)
    terminal_name           = models.CharField(max_length = 10, null = True, blank = True)
    terminal_factory_number = models.CharField(max_length = 10, null = True, blank = True)
    IMEI                    = models.IntegerField(null = True, blank = True)
    activation_time         = models.DateField(auto_now = False, null = True, blank = True)
    now_ip                  = models.CharField(max_length = 30, null = True, blank = True)
    software_version        = models.SmallIntegerField(null = True, blank = True)
    echo_data_content       = models.CharField(max_length = 255, null = True, blank = True)
    factory_time            = models.DateField(auto_now = False, null = True, blank = True)
    main_board_number       = models.SmallIntegerField(null = True, blank = True)
    hardware_version        = models.SmallIntegerField(null = True, blank = True)
    last_fresh_time         = models.DateTimeField(auto_now = True, null = True, blank = True)

class Engine(models.Model):
    """发动机"""
    engine_id              = models.IntegerField(primary_key = True)
    ship_id                = models.ForeignKey(Ship, related_name='ship_about_engine')
    inship_name            = models.CharField(max_length = 20, null = True, blank = True)#船舶中的名称
    brand                  = models.CharField(max_length = 20, null = True, blank = True)
    order_number           = models.CharField(max_length = 20, null = True, blank = True)
    engine_model           = models.CharField(max_length = 20, null = True, blank = True)
    engine_manufacturer    = models.CharField(max_length = 20, null = True, blank = True)
    engine_factory_number  = models.CharField(max_length = 20, null = True, blank = True)
    engine_production_date = models.DateField(null = True, blank = True)
    rated_power            = models.IntegerField(null = True, blank = True)
    rated_speed            = models.SmallIntegerField(null = True, blank = True)

    def __unicode__(self):
        msg = '%s:%s'%(self.inship_name, self.ship_id)
        return msg

class Monitor(models.Model):
    """监控仪"""
    engine_id                 = models.ForeignKey(Engine, related_name='engine_about_monitor')
    monitor_state_protocol_id = models.ForeignKey(Protocol,related_name='monitor_state_protocol_about_monitor')
    para_setting_protocol_id  = models.ForeignKey(Protocol,related_name='para_setting_protocol_about_monitor')
    engine_data_protocol_id   = models.ForeignKey(Protocol,related_name='engine_data_protocol_about_monitor')
    show_name                 = models.CharField(max_length = 10, null = True, blank = True)
    manufacturer              = models.CharField(max_length = 30, null = True, blank = True)
    factory_time              = models.DateField(null = True, blank = True)
    factory_number            = models.CharField(max_length = 20, null = True, blank = True)
    model                     = models.CharField(max_length = 20, null = True, blank = True)
    software_version          = models.SmallIntegerField(null = True, blank = True)
    hardware_version          = models.CharField(max_length = 2, null = True, blank = True)
    ship_inspection           = models.CharField(max_length = 10, null = True, blank = True)
    with_model                = models.CharField(max_length = 20, null = True, blank = True)
    part_number               = models.CharField(max_length = 20, null = True, blank = True)
    sale_customer             = models.CharField(max_length = 20, null = True, blank = True)
    last_customer             = models.CharField(max_length = 20, null = True, blank = True)
    buy_time                  = models.DateField(null = True, blank = True)
    activation_time           = models.DateField(null = True, blank = True)
    warranty_expiration_time  = models.DateField(null = True, blank = True)
    main_board_number         = models.IntegerField(null = True, blank = True)
    final_inspection          = models.CharField(max_length = 10, null = True, blank = True)
    general_assembly          = models.CharField(max_length = 10, null = True, blank = True)
    order_processing          = models.CharField(max_length = 20, null = True, blank = True)

    def __unicode__ (self):
        msg = '%s:%s'%(self.show_name, self.engine_id)
        return msg

class Switch(models.Model):
    """开关量"""
    monitor_id                 = models.ForeignKey(Monitor, related_name='monitor_about_switch')
    time                       = models.DateField()
    alarm                      = models.BooleanField()
    parking                    = models.BooleanField()
    emergency_stop             = models.BooleanField()
    water_temperature_high     = models.BooleanField()
    oil_temperature_high       = models.BooleanField()
    oil_pressure_low           = models.BooleanField()
    speed_high                 = models.BooleanField()
    water_temperature_too_high = models.BooleanField()
    oil_temperature_too_high   = models.BooleanField()
    oil_pressure_too_low       = models.BooleanField()
    speed_too_high             = models.BooleanField()
    stop_button                = models.BooleanField()
    emergency_stop_button      = models.BooleanField()
    start_button               = models.BooleanField()
    mute_button                = models.BooleanField()

class Engine_data(models.Model):
    """发动机数据"""
    receive_data_id      = models.IntegerField(primary_key = True)
    monitor_id           = models.ForeignKey(Monitor, related_name='monitor_about_engine_data')
    generation_data_time = models.DateTimeField(auto_now = False)
    receive_data_time    = models.DateTimeField(auto_now = True)
    speed                = models.DecimalField(max_digits=5, decimal_places=1, null = True, blank = True) #十进制数据的大小问题
    water_temperature    = models.SmallIntegerField(null = True, blank = True)
    oil_temperature      = models.SmallIntegerField(null = True, blank = True)
    oil_pressure         = models.FloatField(null = True, blank = True)
    others               = models.CharField(max_length = 255, null = True, blank = True)

class Monitor_par(models.Model):
    """监控仪的参数"""
    monitor_id  = models.ForeignKey(Monitor, related_name='monitor_about_monitor_par')
    update_time = models.DateTimeField(auto_now = True)
    others      = models.CharField(max_length = 1000, null = True, blank = True)

class Reengine_data(models.Model):
    """推荐发动机数据"""
    recommend_engine_id        =models.IntegerField(primary_key = True)
    engine_id                  = models.ForeignKey(Engine,related_name='engine_about_reengine_data')
    recommend_speed            = models.SmallIntegerField(null = True, blank = True)
    recommed_water_temperature = models.SmallIntegerField(null = True, blank = True)
    recommed_oil_temperature   = models.SmallIntegerField(null = True, blank = True)
    recommed_speed             = models.FloatField(null = True, blank = True)
    time                       = models.DateTimeField(auto_now = True, null = True, blank = True)

class Gear(models.Model):
    """齿轮箱"""
    gear_id                     = models.IntegerField(primary_key = True)
    ship_id                     = models.ForeignKey(Ship, related_name='ship_about_gear')
    gear_name                   = models.CharField(max_length = 20)
    gear_model                  = models.CharField(max_length = 20, null = True, blank = True)
    protocol_id                 = models.ForeignKey(Protocol, related_name='protocol_about_gear')
    gear_manufacturer           = models.CharField(max_length = 50, null = True, blank = True)
    gear_factory_number         = models.CharField(max_length = 20, null = True, blank = True)
    gear_factory_time           = models.DateField(auto_now = False, null = True, blank = True)
    deceleration_velocity_ratio = models.FloatField(null = True, blank = True)

    def __unicode__(self):
        return self.gear_name

class Gear_data(models.Model):
    """齿轮箱数据"""
    gear_id              = models.ForeignKey(Gear, related_name='gear_about_gear_data')
    generation_data_time = models.DateTimeField(auto_now = False, null = True, blank = True)
    oil_pressure         = models.FloatField(null = True, blank = True)
    roller_oil_pressure  = models.FloatField(null = True, blank = True)
    others               = models.CharField(max_length = 1000, null = True, blank = True)

class Boiler(models.Model):
    """锅炉"""
    boiler_id      = models.IntegerField(primary_key = True)
    ship_id        = models.ForeignKey(Ship, related_name='ship_about_boiler')
    boiler_name    = models.CharField(max_length = 20, null = True, blank = True)
    boiler_model   = models.CharField(max_length = 20, null = True, blank = True)
    factory_number = models.CharField(max_length = 20, null = True, blank = True)
    protocol_id    = models.ForeignKey(Protocol, related_name='protocol_about_boiler')

    def __unicode__(self):
        return self.boiler_name

class Boiler_data(models.Model):
    """锅炉数据"""
    boiler_id            =models.ForeignKey(Boiler, related_name='boiler_about_boiler_data')
    generation_data_time = models.DateTimeField(auto_now =False)
    boiler_temperature   = models.SmallIntegerField()
    others               = models.CharField(max_length = 255)



class Propeller(models.Model):
    """螺旋桨"""
    propeller_id    = models.IntegerField(primary_key = True)
    ship_id         = models.ForeignKey(Ship)
    propeller_name  = models.CharField(max_length = 10, null = True, blank = True)
    propeller_pitch = models.FloatField(null = True, blank = True)
    blade_number    = models.IntegerField(null = True, blank = True)# tinyint
    diameter        = models.FloatField(null = True, blank = True)
    post_dip        = models.FloatField(null = True, blank = True)



class Voyage_number(models.Model):
    """航次"""
    ship_id                  = models.ForeignKey(Ship)
    start_time               = models.DateTimeField(auto_now = False, null = True, blank = True)
    end_time                 = models.DateTimeField(auto_now = False, null = True, blank = True)
    satrt_position_longitude = models.DecimalField(max_digits=9, decimal_places=2, null = True, blank = True)
    satrt_position_latitude  = models.DecimalField(max_digits=9, decimal_places=2, null = True, blank = True)
    satrt_position_alititude = models.DecimalField(max_digits=5, decimal_places=2, null = True, blank = True)
    end_position_longitude   = models.DecimalField(max_digits=9, decimal_places=2, null = True, blank = True)
    end_position_latitude    = models.DecimalField(max_digits=9, decimal_places=2, null = True, blank = True)
    end_position_alititude   = models.DecimalField(max_digits=5, decimal_places=2, null = True, blank = True)
    voyage                   = models.FloatField(null = True, blank = True)
    voyage_time              = models.DateTimeField(null = True, blank = True)
    average_speed            = models.FloatField(null = True, blank = True)
    average_oil              = models.FloatField(null = True, blank = True)
    oil_consum               = models.FloatField(null = True, blank = True)
    driver_name              = models.CharField(max_length = 255, null = True, blank = True)
    average_voyage_par       = models.BinaryField(null = True, blank = True)# 存储文件，不是最好的决定

class Engine_fault_record(models.Model):
    """发动机的故障记录"""
    engine_id       = models.ForeignKey(Engine)
    generation_time = models.DateTimeField(auto_now = False, null = True, blank = True)
    fault_name      = models.CharField(max_length = 255, null = True, blank = True)
    fault_cause     = models.CharField(max_length = 255, null = True, blank = True)
    fault_type      = models.CharField(max_length = 255, null = True, blank = True)
    solution_method = models.CharField(max_length = 255, null = True, blank = True)
    fault_source    = models.CharField(max_length = 255, null = True, blank = True)
    treament_result = models.CharField(max_length = 255, null = True, blank = True)